<template>
  <div class="app-container">
    <el-card class="query-card">
      <el-form ref="query" :model="page.query" inline label-position="right" label-width="90px">
        <el-form-item label="生产日期" prop="outputDateRange">
          <el-date-picker
            v-model="page.query.outputDateRange"
            type="daterange"
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
            value-format="yyyy-MM-dd"
          />
        </el-form-item>
        <el-form-item label="产品名称" prop="productId">
          <product-query ref="productQuery" v-model="page.query.productId" @choose="handleChooseProduct" />
        </el-form-item>
        <el-form-item label="排产编号" prop="produceNo">
          <el-input v-model="page.query.produceNo" placeholder="请输入排产编号" />
        </el-form-item>
        <el-form-item label="产量编号" prop="outputNo">
          <el-input v-model="page.query.outputNo" placeholder="请输入产量编号" />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" @click="handleSearch">查询</el-button>
          <el-button icon="el-icon-refresh" @click="resetQueryForm">重置</el-button>
        </el-form-item>
      </el-form>
    </el-card>
    <el-card class="table-card">
      <el-tabs v-model="page.query.shiftId" @tab-click="handleTabClick">
        <el-tab-pane
          v-for="(item,index) in shiftCountList"
          :key="index"
          :label="item.stateName+'('+item.count+')'"
          :name="item.state"
        />
      </el-tabs>
      <!--      <div>
        <span>产出信息列表</span>
      </div>
      <el-divider />-->
      <div style="height: 50px">
        <div v-for="(item,index) in outputQuantityList" :key="index" style="display: inline">
          <div class="output_title">总产量:</div>
          <div class="output_quantity" style="margin-left: 10px">{{ item.count }}</div>
          <div class="output_quantity" style="margin-left: 3px">{{ item.state }}</div>
        </div>
      </div>
      <div class="table-button">
        <el-button type="primary" class="el-icon-plus" @click="saveClick">新增</el-button>
        <el-button type="warning" class="el-icon-edit" plain @click="updateClick('tableBtn')">修改</el-button>
        <el-button type="danger" plain class="el-icon-folder-delete" @click="handleBatchDelete">
          批量删除
        </el-button>
        <el-tooltip content="刷新" class="table-right-btn">
          <el-button size="mini" circle icon="el-icon-refresh" @click="doPage" />
        </el-tooltip>
      </div>
      <el-table
        v-loading="tableLoading"
        :data="tableData"
        style="width: 100%"
        border
        @selection-change="handleSelectionChange"
      >
        <el-table-column type="selection" width="45" />
        <el-table-column label="产量编号" prop="outputNo" min-width="100" />
        <el-table-column label="生产日期" prop="outputDate" min-width="100" />
        <el-table-column label="排产编号" prop="produceNo" min-width="110" />
        <el-table-column label="产品名称" prop="productName" min-width="200" />
        <el-table-column label="班次" prop="shiftName" />
        <el-table-column label="产量" prop="quantity" />
        <el-table-column label="操作" prop="operation" fixed="right" width="100">
          <template slot-scope="scope">
            <el-button
              type="text"
              @click="updateClick('column',scope.row)"
            >修改
            </el-button>
            <el-popconfirm
              confirm-button-text="确定"
              cancel-button-text="取消"
              placement="top"
              style="margin-left: 10px"
              title="是否删除此条数据"
              @confirm="handleDelConfirm(scope.row)"
            >
              <el-button slot="reference" type="text">删除</el-button>
            </el-popconfirm>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        :page-sizes="[10, 20, 30, 40]"
        :page-size="10"
        layout="total, sizes, prev, pager, next, jumper"
        :total="page.total"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </el-card>
    <div v-if="dialogVisible">
      <submit-module ref="submitModule" :visible="dialogVisible" @close="handleClose" />
    </div>
  </div>
</template>

<script>
import { deleteOutputById, selectOutputPage, selectOutputQuantity, selectShiftCount } from '@/api/erp/output'
import SubmitModule from '@/views/erp/output/modules/submitModule.vue'

export default {
  name: 'Index',
  components: { SubmitModule },
  data() {
    return {
      tableData: [],
      tableLoading: false,
      selected: [],
      dialogVisible: false,
      shiftCountList: [],
      outputQuantityList: [],
      page: {
        page: 1,
        size: 10,
        total: 0,
        query: {
          outputDateRange: [],
          shiftId: '-1',
          productName: '',
          produceNo: '',
          outputNo: ''
        }
      }
    }
  },
  mounted() {
    this.doPage()
  },
  methods: {
    handleSearch() {
      this.page.page = 1
      this.doPage()
    },
    doPage() {
      // 分页查询
      this.tableLoading = true
      selectOutputPage(this.page).then(resp => {
        if (resp.success) {
          this.tableData = resp.data.records
          this.page.total = resp.data.total
        }
      }).finally(() => {
        this.tableLoading = false
      })
      this.selectOutputQuantity()
      this.selectShiftCount()
    },
    // 选中事件
    handleSelectionChange(row) {
      this.selected = row
    },
    resetQueryForm() {
      this.$refs.query.resetFields()
      this.page.query.productId = undefined
      this.page.query.shiftId = '-1'
      this.$refs.productQuery.resetQueryForm()
      this.doPage()
    },
    // 分页大小
    handleSizeChange(val) {
      this.page.size = val
      this.doPage()
    },
    // 页码
    handleCurrentChange(val) {
      this.page.page = val
      this.doPage()
    },
    // 新增按钮点击
    saveClick() {
      this.dialogVisible = true
      this.$nextTick(() => {
        this.$refs.submitModule.init()
      })
    },
    // 修改按钮点击
    updateClick(type, row) {
      let id = ''
      if (type === 'column') {
        id = row.id
      } else {
        if (this.selected.length !== 1) {
          this.$message.warning('请选择一条需要修改的数据')
          return
        }
        id = this.selected[0].id
      }
      this.dialogVisible = true
      this.$nextTick(() => {
        this.$refs.submitModule.init(id)
      })
    },
    // 批量删除点击
    handleBatchDelete() {
      if (this.selected.length === 0) {
        this.$message.warning('请选择一条或多条数据')
        return
      }
      this.$confirm('是否批量删除选中的数据?', '删除提醒', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'error'
      }).then(() => {
        this.tableLoading = true
        const data = { ids: this.selected.map(item => item.id) }
        deleteOutputById(data).then(resp => {
          if (resp.success) {
            this.$message.success('删除成功')
          }
        }).finally(() => {
          this.tableLoading = false
          this.doPage()
        })
      })
    },
    handleDelConfirm(row) {
      this.tableLoading = true
      const data = { ids: [row.id] }
      deleteOutputById(data).then(resp => {
        if (resp.success) {
          this.$message.success('删除成功')
          this.doPage()
        }
      }).finally(() => {
        this.tableLoading = false
      })
    },
    handleClose() {
      this.dialogVisible = false
      this.doPage()
    },
    handleChooseProduct(row) {
      if (row) {
        this.page.query.productId = row.id
      }
    },
    handleTabClick(tab) {
      this.page.query.shiftId = tab.name
      this.doPage()
    },
    selectShiftCount() {
      this.tableLoading = true
      selectShiftCount(this.page.query).then(resp => {
        if (resp.success) {
          this.shiftCountList = resp.data
        }
      }).finally(() => {
        this.tableLoading = false
      })
    },
    selectOutputQuantity() {
      this.tableLoading = true
      selectOutputQuantity(this.page.query).then(resp => {
        if (resp.success) {
          this.outputQuantityList = resp.data
        }
      }).finally(() => {
        this.tableLoading = false
      })
    }
  }
}
</script>

<style scoped>
.output_quantity {
  display: inline;
  font-size: 20px;
  color: #ff4949;
}

.output_title {
  color: #515a6e;
  display: inline;
  font-size: 16px
}
</style>
